from collections.abc import Sequence
from typing import Any
from django.contrib import admin
from django.http import HttpRequest
from .models import Country, Province, Area, City

# Register your models here.
class ReadOnlyAdmin(admin.ModelAdmin):
    readonly_fields = []
    def get_readonly_fields(self, request: HttpRequest, obj: Any | None = ...) -> list[str] | tuple[Any, ...]:
        return list(self.readonly_fields) + \
            [field.name for field in obj._meta.fields] + \
            [field.name for field in obj._meta.many_to_many]
    
    def get_list_display(self, request: HttpRequest) -> Sequence[str]:
        return [field.name for field in self.model._meta.concrete_fields]
    
    def has_add_permission(self, request):
        return False
    
    def has_delete_permission(self, request, obj=None):
        return False
    
    def has_change_permission(self, request, obj=None):
        return False
    


@admin.register(Country)
class CountryAdmin(ReadOnlyAdmin):
    search_fields = ('chn_name', 'eng_name',)

@admin.register(Province)
class ProvinceAdmin(ReadOnlyAdmin):
    search_fields = ('chn_name', 'eng_name',)


class CityAdmin(ReadOnlyAdmin):
    autocomplete_fields = ['provinceid', 'countryid',]
    list_display = ('cityid', 'countryid', 'areaid', 'provinceid', 'chn_name', 'eng_name',)


# admin.site.register(Country)
# admin.site.register(Province)
admin.site.register(Area)
admin.site.register(City, CityAdmin)


# 47| Django之美：10行代码自动注册所有Model到Admin管理后台
